import Taro from '@tarojs/taro'
import {View} from '@tarojs/components'
import {AtActivityIndicator} from 'taro-ui'
import {connect} from "@tarojs/redux";
import ProjectItem from '../../components/project_item'
import Empty from '../../components/empty'
import withShare from "../../components/share/withShare";

import './index.less'

@connect(state => ({
  loading: state.loading.effects['project/search'],
  pca: state.pca,
  project: state.project,
}))
@withShare()
export default class SearchBar extends Taro.Component {

  constructor () {
    super(...arguments)

    // TODO 显示搜索项
    this.state = {
      // showValue: ''
    }
  }

  componentDidMount() {
    const {dispatch, pca: {selectedPcaNode}} = this.props;
    const cid = Number(this.$router.params.cid);
    const name = this.$router.params.name;
    let payload = {};
    let searchTextArray = [];

    searchTextArray.push(selectedPcaNode.name);
    if (cid) {
      const cname = this.$router.params.cname;
      searchTextArray.push(cname);
      payload.cid = cid;
    }
    if (selectedPcaNode.id) {
      payload.pca_id = selectedPcaNode.id;
    }
    if (name) {
      // searchTextArray.push(name);
      payload.name = name;
    }
    searchTextArray.push('项目');

    dispatch({
      type: 'project/search',
      payload,
    });

    Taro.setNavigationBarTitle({
      title: searchTextArray.join(' '),
    });
  }

  // TODO 无数据时显示
  render() {
    const {loading, project: {list}} = this.props;

    return (
      <View className='container'>
        <View className='panel'>
          <View className='body'>
            {
              list.length > 0
              ? list.map(item => <ProjectItem key={item.id} item={item}/>)
              : loading ? <AtActivityIndicator mode='center'/> : <Empty/>
            }
          </View>
        </View>
      </View>
    )
  }
}
